import {
  feeSaveOrder,
  getOrderAllFeeInfoBeforeEdit,
  getOrderDetailFeeProjectList,
  getOrderFeeDetail,
  OrderFeeDetailProjectInfo,
  updateOrderDetailFee,
  workFeeProjectPageSelect,
} from '@/api/order'
import { ref } from 'vue'

/**
 * 获取工单详情的费用列表
 */
export const useGetOrderDetailFees = () => {
  const getFeesLoading = ref(false)
  const getFeesParam = ref({
    limit: 50,
    page: 1,
  })
  const fees = ref<Array<OrderFeeAPIResponse.OrderDetailFeeListItem>>([])
  const feesTotal = ref(0)

  const getFees = async (orderId: string) => {
    try {
      getFeesLoading.value = true
      const res = await getOrderDetailFeeProjectList(getFeesParam.value, orderId)
      getFeesLoading.value = false
      if (res.data.code === 20000) {
        fees.value = res.data.data.rows
        feesTotal.value = Number(res.data.data.total)
        return ''
      }
      return res.data.message
    } catch (error) {
      getFeesLoading.value = false
      return '获取工单费用列表失败，请稍后重试'
    }
  }

  return {
    fees,
    feesTotal,
    getFeesParam,
    getFeesLoading,
    getFees,
  }
}

/**
 * 工单详情的费用表单服务
 */
export const useOrderDetailFeeForm = () => {
  const feeFormSaveLoading = ref(false)
  const feeForm = ref<OrderFeeAPIRequest.OrderDetailFeeForm>({
    delFeeDetailIds: [],
    feeDetails: [],
    id: '',
    remark: '',
    workId: '',
  })

  const reserForm = () => {
    feeForm.value = {
      delFeeDetailIds: [],
      feeDetails: [],
      id: '',
      remark: '',
      workId: '',
    }
  }

  const formSave = async () => {
    try {
      feeFormSaveLoading.value = true
      const res = await feeSaveOrder(feeForm.value)
      feeFormSaveLoading.value = false
      return res.data
    } catch (error) {
      feeFormSaveLoading.value = false
      return {
        message: '保存失败，请稍后重试',
        code: 1000,
        success: false,
        hasOtherCode: false,
        otherCodePrefix: null,
        data: null,
      }
    }
  }

  return {
    feeForm,
    feeFormSaveLoading,
    reserForm,
    formSave,
  }
}

/**
 * 获取工单费用下拉选项
 * 工单设置里面的工单费用
 */
export const useGetFeeOptions = () => {
  const getFeeOptionsLoading = ref(false)
  const getFeeOptionsParam = ref<OrderFeeAPIRequest.GetFeeOptionsParam>({
    limit: 50,
    page: 1,
    name: '',
  })
  const feeOptions = ref<Array<OrderFeeAPIResponse.OrderFeeOption>>([])

  const getFeeOptions = async () => {
    try {
      getFeeOptionsLoading.value = true
      const res = await workFeeProjectPageSelect(getFeeOptionsParam.value)
      getFeeOptionsLoading.value = false
      if (res.data.code === 20000) {
        feeOptions.value = res.data.data.rows
        return ''
      }
      return res.data.message
    } catch (error) {
      getFeeOptionsLoading.value = false
      return '获取工单费用下拉选项失败，请稍后重试'
    }
  }

  return {
    feeOptions,
    getFeeOptionsParam,
    getFeeOptionsLoading,
    getFeeOptions,
  }
}

/**
 * 获取工单详情的费用详情
 */
export const useGetOrderDetailFeeDetail = () => {
  const getFeeDetailLoading = ref(false)
  const feeDetail = ref<OrderFeeAPIResponse.OrderDetailFeeDetail>({
    applyEmp: '',
    applyEmpId: '',
    applyEmpType: 1,
    applyRemark: '',
    createTime: '',
    feeNo: '',
    id: '',
    receive: false,
    reviewEmp: '',
    reviewRemark: '',
    reviewTime: '',
    settlementType: '',
    status: 1,
    surePrice: false,
    totalPrice: 0,
    updateTime: '',
    workNo: '',
  })

  const getFeeDetail = async (feeId: string) => {
    try {
      getFeeDetailLoading.value = true
      const res = await getOrderFeeDetail(feeId)
      getFeeDetailLoading.value = false
      if (res.data.code === 20000) {
        feeDetail.value = res.data.data.row
        return ''
      }
      return res.data.message
    } catch (error) {
      getFeeDetailLoading.value = false
      return '获取费用详情失败，请稍后重试'
    }
  }

  return {
    feeDetail,
    getFeeDetailLoading,
    getFeeDetail,
  }
}

/**
 * 获取工单详情的费用详情的项目列表
 */
export const useGetOrderDetailFeeDetailProjectList = () => {
  const getFeeProjectListLoading = ref(false)
  const feeProjectList = ref<Array<OrderFeeAPIResponse.OrderDetailFeeDetailProject>>([])

  const getFeePorjectList = async (feeId: string) => {
    try {
      getFeeProjectListLoading.value = true
      const res = await OrderFeeDetailProjectInfo(feeId)
      getFeeProjectListLoading.value = false
      if (res.data.code === 20000) {
        feeProjectList.value = res.data.data.row
        return ''
      }
      return res.data.message
    } catch (error) {
      getFeeProjectListLoading.value = false
      return '获取费用项目失败，请稍后重试'
    }
  }

  return {
    feeProjectList,
    getFeeProjectListLoading,
    getFeePorjectList,
  }
}

/**
 * 获取编辑之前的工单费用（获取全部的）
 */
export const useGetOrderFeeInfoBeforeEdit = () => {
  const getFeeInfoLoading = ref(false)
  const feeInfo = ref<OrderFeeAPIResponse.FeeInfoBeforeEdit>({
    applyRemark: '',
    createTime: '',
    feeDetailInfoVos: [],
    id: '',
    status: 2,
    workFeeNo: '',
  })

  const getFeeInfo = async (orderId: string, feeId: string) => {
    try {
      getFeeInfoLoading.value = true
      const res = await getOrderAllFeeInfoBeforeEdit(orderId)
      getFeeInfoLoading.value = false
      if (res.data.code === 20000) {
        const fee = res.data.data.rows.find((item) => item.id === feeId)
        if (fee) {
          feeInfo.value = fee
        }
        return ''
      }
      return res.data.message
    } catch (error) {
      return '获取费用信息失败，请稍后重试'
    }
  }

  return {
    feeInfo,
    getFeeInfoLoading,
    getFeeInfo,
  }
}

/**
 * 更新工单详情的费用信息
 */
export const useUpdateOrderDetailFee = () => {
  const updateLoading = ref(false)
  const updateForm = ref<OrderFeeAPIRequest.UpdateOrderDetailFee>({
    checkVoList: [],
  })

  const update = async () => {
    try {
      updateLoading.value = true
      const res = await updateOrderDetailFee(updateForm.value)
      updateLoading.value = false
      return res.data
    } catch (error) {
      return {
        code: 1000,
        message: '更新工单费用失败，请稍后重试',
        success: false,
        data: {
          row: {},
        },
        hasOtherCode: false,
        otherCodePrefix: null,
      }
    }
  }

  return {
    updateForm,
    updateLoading,
    update,
  }
}
